package com.zygame.sdk.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zygame.sdk.CmCode;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.LoginUtil;
import com.zygame.sdk.utils.http.HttpUtil;
import com.zygame.sdk.utils.http.NetHttpUtil.DataCallback;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.phone.AppUtil;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.utils.toast.ToastUtil;
import com.zygame.sdk.view.ImageEditText;

import org.json.JSONObject;

/**
 * Yuxl
 * 注册
 */
public class ZY_Dialog_Register extends ZY_BaseDialog {

    private Context mContext;

    private String mAccount;
    private String mPwd;

    //	private ImageView tv_close;
    private ImageEditText et_account;
    private ImageEditText et_pwd;
    private ImageView fy_delete_username;
    private ImageView fy_delete_pwd;
    private Button btn_register;
    private LinearLayout ll_treaty;
    private Button btn_has_account;
    private TextView tv_phone_register;
    private TextView tv_quick_register;

    private TextView btn_back;

    public ZY_Dialog_Register(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void init() {
        //统计注册页面
        showSoft();
//        HttpUtil.staRegisterPage(mContext, null);
        showBack(true);
        showClose(true);
//		tv_close = (ImageView) findViewById(ResourceUtil.getId(mContext, "tv_close"));
        et_account = (ImageEditText) findViewById(ResourceUtil.getId(mContext, "et_account"));
        et_pwd = (ImageEditText) findViewById(ResourceUtil.getId(mContext, "et_pwd"));

        et_account.setTextViewBG("fy_input_account");
        et_account.setHintid("GSDK_email_text");
        et_account.setLineHide();
        et_account.setContentClole("#000000");
        et_account.hideHidstoryImageView();
        et_account.hideDelete();
        et_account.hidePromptText();

        et_pwd.setTextViewBG("fy_input_pwd");
        et_pwd.setHintid("GSDK_password_placeholaer");
        et_pwd.hidePromptGone();
        et_pwd.hideHidstoryImageView();
        et_pwd.setLineHide();
        et_pwd.setContentClole("#000000");
        et_pwd.setIcon(mContext.getResources().getDrawable(ResourceUtil.getDrawableId(mContext, "fy_pwd")));
        et_pwd.hideDelete();

        Button btn_login = (Button) findViewById(ResourceUtil.getId(mContext, "btn_login"));
        findViewById(ResourceUtil.getId(mContext, "tv_quick_register")).setOnClickListener(this);

        btn_login.setOnClickListener(this);
    }


    @Override
    protected String getResourceStr() {
        return "zy_dialog_register";
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        super.onClick(v);
        int id = v.getId();
        if (id == ResourceUtil.getId(mContext, "btn_back")) {
             Zy_Dialog_main.getInstance(mContext).show();
            ZyGameSDK.getInstance().setDialogDismissFlag(CmCode.CODE_LOGIN_CANCEL);
            ZY_Dialog_Register.this.dismiss();
        }

        if (id == ResourceUtil.getId(mContext, "tv_close")) {
//			Game65SDK.getInstance().setDialogDismissFlag(_65Code.CODE_REGISTER_CANCEL);
            ZyGameSDK.getInstance().setDialogDismissFlag(CmCode.CODE_LOGIN_CANCEL);
            ZY_Dialog_Register.this.dismiss();
        }  else if (id == ResourceUtil.getId(mContext, "btn_login")) {
            mAccount = et_account.getContent().trim();
            mPwd = et_pwd.getContent().trim();
            if (mAccount.isEmpty() || mPwd.isEmpty()) {
//                ToastUtil.showToast(mContext,"账号和密码不能为空");
                et_account.setPromptTextId("GSDK_UserApassword_error");
                return;
            }
            if (!AppUtil.isEmail(mAccount)) {
//                ToastUtil.showToast(mContext,"请输入正确的邮箱");
                et_account.setPromptTextId("GSDK_email_error");
                return;
            }
            if (AppUtil.txtLen(mPwd)) {
//                ToastUtil.showToast(mContext,"请输入正确的密码");
                et_account.setPromptTextId("GSDK_password_error");
                return;
            }

            HttpUtil.reg(mContext, onRegCallback, mAccount, mPwd, "1");
            showLoad(true);
        } else if (id == ResourceUtil.getId(mContext, "tv_quick_register")) {
            new ZY_Dialog_QRegister(mContext).show();
            dismiss();
        }

    }


    DataCallback<JSONObject> onRegCallback = new DataCallback<JSONObject>() {
        @Override
        public void callbackSuccess(JSONObject paramObject) {
            showLoad(false);
            LogHelper.i("接口", paramObject.toString());
            if ("0".equals(paramObject.optString("code"))) {


                LoginUtil.loginResult(mAccount, mPwd, paramObject, mContext,"1",1,"");
                ZyGameSDK.getInstance().onLoginResult(UserModel.getInstance());
                dismiss();

            } else {
//                        ToastUtil.showToast(mContext,paramObject.optString("message"));
                et_account.setPromptText(paramObject.optString("message"));
            }
        }

        @Override
        public void callbackError(String error) {
            showLoad(false);
            Toast.makeText(mContext,error,Toast.LENGTH_SHORT).show();
        }
    };





}
